
package org.fintool.parser;

import java.io.BufferedWriter;
import java.io.File;
import java.net.URL;
import java.util.ArrayList;

import org.fintool.common.Constants;
import org.fintool.fileio.FileWriter;
import org.fintool.functional.TransfromData;
import org.fintool.http.HTTPUtil;
import org.fintool.http.URLMaker;

public class CSVParser  implements IParser{









public String getElement(String strURL, String elementDefinition)
		throws Exception {
	// TODO Auto-generated method stub
	return null;
}


public void writeRawFileData(String stock,String exchange,
		ArrayList dateRange) throws Exception {
	// TODO Auto-generated method stub

	ArrayList alRawFileData=null;
	org.fintool.fileio.FileReader fileReader=null;

//for a Simple day file we will get date as null so parse for currentDay and year
if(dateRange==null){
	fileReader=new org.fintool.fileio.FileReader();

	FileWriter fileWriter=new FileWriter();

	long currentTimeStamp=0l;


	//If file not present Create it

		//If file is  present update it with data
		//check timestamp of the file and update the last line
		//allow only the difference to update
	currentTimeStamp=fileReader.getFileLastDate(stock,exchange);
	System.out.println(currentTimeStamp);

	if(currentTimeStamp==0l||currentTimeStamp>0){//No content write all,for now over write all

		alRawFileData=getHTTPContentData(stock, exchange);
		//	fileWriter.
			fileWriter.createFile(stock, exchange);
		//fileWriter
		File fileHandle=new File(fileReader.getFileDirPath(stock, exchange)+File.separator
				+fileReader.getFileLastDate(stock, exchange));

		if(fileHandle!=null){
		 BufferedWriter fileBuffer = new BufferedWriter(new java.io.FileWriter(fileHandle));
		 if(fileBuffer!=null){

			 fileWriter.writeToFileandClose(fileHandle,fileBuffer, alRawFileData);

			 fileWriter.updateFileTS(stock, exchange);
		 }
		}
	}else{

		//Start coding here

		//append new line for  separating latest
		//get differential data for stock ,i.e data from last timestamp
		   //Get URL for finding data between dates
		//If need be Intersect overwrite


	}


}
//Else use URL maker with timestamp data
else{

}

//After writing write to another file with transformed data
writeRawFileDataTransfromedDate(stock,exchange,null);
}


public void writeRawFileDataTransfromedDate(String stock,String exchange,
		ArrayList dateRange) throws Exception {

	ArrayList alRawFileData=null;
	org.fintool.fileio.FileReader fileReader=null;
	ArrayList alTransformData=null;

//for a Simple day file we will get date as null so parse for currentDay and year
if(dateRange==null){
	fileReader=new org.fintool.fileio.FileReader();

	FileWriter fileWriter=new FileWriter();

	long currentTimeStamp=0l;

	String strCurrentFilePath=null;


	//If file not present Create it

		//If file is  present update it with data
		//check timestamp of the file and update the last line
		//allow only the difference to update
	currentTimeStamp=fileReader.getFileLastDate(stock,exchange);
	//System.out.println(currentTimeStamp);
	strCurrentFilePath=fileReader.getFileDirPath(stock, exchange);

	if(currentTimeStamp==0l||currentTimeStamp>0){//No content write all,for now over write all

		//alRawFileData=getHTTPContentData(stock, exchange);

		alRawFileData=fileReader.getRawDataAsLines(strCurrentFilePath+File.separator
				+fileReader.getFileLastDate(stock, exchange));

		alTransformData=TransfromData.transRawtoProperDates(alRawFileData);


		//	fileWriter.
			fileWriter.createTransFormFile(stock, exchange);
		//fileWriter
		File fileHandle=new File(fileReader.getTransFormFileDirPath(stock, exchange)+File.separator
				+fileReader.getTransformFileLastDate(stock, exchange));

		if(fileHandle!=null){
		 BufferedWriter fileBuffer = new BufferedWriter(new java.io.FileWriter(fileHandle));
		 if(fileBuffer!=null){

			 fileWriter.writeToFileandClose(fileHandle,fileBuffer, alTransformData);

			 fileWriter.updateTransformFileTS(stock, exchange);
		 }
		}
	}else{

		//Start coding here

		//append new line for  separating latest
		//get differential data for stock ,i.e data from last timestamp
		   //Get URL for finding data between dates
		//If need be Intersect overwrite


	}


}
//Else use URL maker with timestamp data
else{

}





}
//Main method
private final  ArrayList getHTTPContentData(String stock,String exchange) {
	return getHTTPContentData(stock, exchange, null);

}
/**
 * @param stock
 * @param exchange
 * @param dateRange
 * @return
 */
//This will filter out Lines and get only comma separated  lines
private final  ArrayList getHTTPContentData(String stock,String exchange,ArrayList dateRange) {

	ArrayList httpLines=new ArrayList();

	try{

		URL url=new URL( new URLMaker().getURL(stock, exchange));
		String strFullPage=null;
		String strLines[]=null;
		if(url!=null){
			strFullPage=new HTTPUtil().getHTTPContentAsString(url);
			strLines=strFullPage.split("\r\n");
			for(int i=0;i<strLines.length;i++){

				if(strLines[i].matches(Constants.GFINANCE_LINEREGEX)){
					if(!strLines[i].equalsIgnoreCase(Constants.GFINANCE_COLUMN_HEADER)){
					//System.out.println(strLines[i]);
					httpLines.add(strLines[i]);
					}

				}

			}



		}

	}catch (Exception e) {
		e.printStackTrace();
		// TODO: handle exception
	}



	return httpLines;

}

/* (non-Javadoc)
 * @see org.fintool.parser.IParser#getRawFileData(java.lang.String, java.lang.String, java.util.ArrayList)
 */
//Do not do hTTP here this is only for file reads
public ArrayList getRawFileData(String stock,String exchange,
			ArrayList dateRange) throws Exception {
		// TODO Auto-generated method stub

	ArrayList alRawFileData=null;

	//for a Simple day file we will get date as null so parse for currentDay and year
	if(dateRange==null){


	}
	//Else use URL maker with timestamp data
	else{

	}
		return alRawFileData;
}
	//Main method


	public static void main(String[] args) throws Exception {
		CSVParser pr=new CSVParser();

	//	System.out.println(new HTTPUtil().getHTTPContentAsString(new URL("http://www.google.com/finance/getprices?q=NCM&x=ASX&i=86400&p=10d&f=d,c,v,o,h,l&df=cpct&auto=1&ts=1301712656355")));

		//System.out.println(new HTTPUtil().getHTTPContentAsString(new URL(new URLMaker().getURL("NCM", "ASX"))));
		//System.out.println(pr.findMatchedString("chr io","chr"));
		//new CSVParser().getHTTPContentData("NCM", "ASX", null);

		CSVParser csvParser=new CSVParser();
		csvParser.writeRawFileData("NCM", "ASX", null);
		csvParser.writeRawFileData("DJUSPM", "INDEXDJX", null);
		//INDEXDJX

	}




}
